package com.woniu108.product.service.impl;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu108.carService.commos.dto.ScoreStoreDto;
import com.woniu108.product.service.ScoreStoreService;
import org.springframework.stereotype.Service;
import com.woniu108.product.mapper.ScoreStoreMapper;
import com.woniu108.product.model.ScoreStore;
import com.woniu108.product.param.ScoreProductListParam;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 积分商城表 服务实现类
 * </p>
 *
 * @author yanxi
 * @since 2024-05-23
 */
@Service
public class ScoreStoreServiceImpl extends ServiceImpl<ScoreStoreMapper, ScoreStore> implements ScoreStoreService {

    @Resource
    private ScoreStoreMapper scoreStoreMapper;
    @Override
    public Page<ScoreStoreDto> scoreProductList(ScoreProductListParam param) {
        QueryWrapper<ScoreStore> wrapper = new QueryWrapper<>();
        String scoreProductName = param.getScoreProductName();
        BigDecimal lowScorePrice = param.getLowScorePrice();
        BigDecimal upScorePrice = param.getUpScorePrice();
        if(scoreProductName!=null) wrapper.like("score_product_name",scoreProductName);
        if(lowScorePrice!=null) wrapper.ge("score_price",lowScorePrice);
        if(upScorePrice!=null) wrapper.le("score_price",upScorePrice);
        List<ScoreStore> storeList = scoreStoreMapper.selectList(wrapper);
        Page<ScoreStoreDto> page=new Page<ScoreStoreDto>();
        List<ScoreStoreDto> dtos=new ArrayList<>();
        long total = storeList.size();
        long pages=total%param.getPageSize()==0?total/param.getPageSize():total/param.getPageSize()+1;
        int i=0;
        for (ScoreStore scoreStore : storeList) {
            if(i<param.getPageNum()*param.getPageSize().intValue()&&i>=(param.getPageNum()-1)*param.getPageSize().intValue()){
                ScoreStoreDto storeDto = BeanUtil.toBean(scoreStore, ScoreStoreDto.class);
                dtos.add(storeDto);
            }
        }
        page.setCurrent(param.getPageNum());
        page.setSize(param.getPageSize());
        page.setRecords(dtos);
        page.setTotal(total);
        page.setPages(pages);
        return page;
    }
}
